package test29.Date;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;


public class DateTime {
    //LocalDate、LocalTime、LocalDateTime
    //LocalDateTime相对于其他两个用的频率高一点
    @Test
    public void test1(){
        //now():获取当前的日期、时间、日期+时间
        LocalDate localDate= LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);//2025-08-05
        System.out.println(localTime);//17:32:22.238
        System.out.println(localDateTime);//2025-08-05T17:32:22.238

        //of():指定的年月日时分秒是没有偏移量的
        LocalDateTime localDateTime1 = LocalDateTime.of(2025, 8, 5, 17, 39);
        System.out.println(localDateTime1);//2025-08-05T17:39

        //getxxx()
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMinute());
        System.out.println(localDateTime.getMinute());

        //体现不可变性
        //withxxx（）：设置相关属性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate);//2025-08-05
        System.out.println(localDate1);//2025-08-22

        //不可变性
        //加
        LocalDateTime localDateTime2 = localDateTime.plusMinutes(3);
        System.out.println(localDateTime);//2025-08-05T17:47:08.597
        System.out.println(localDateTime2);//2025-08-05T17:49:34.805
        //减
        LocalDateTime localDateTime3 = localDateTime.minusDays(6);
        System.out.println(localDateTime);//2025-08-05T17:48:23.930
        System.out.println(localDateTime3);//2025-07-30T17:48:23.930
    }

    //instant
    //
    @Test
    public void test2(){
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2025-08-05T09:53:48.339Z

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2025-08-05T17:55:48.932+08:00

        //toEpochMilli(): 获取自1970年1月1号0时0分0秒（UTC）开始的毫秒数
        long milli = instant.toEpochMilli();
        System.out.println(milli);//1754387949745

        //ofEpochMilli():通过给定的毫秒数，获取Instant实例
        Instant instant1 = Instant.ofEpochMilli(1754387949745L);
        System.out.println(instant1);//2025-08-05T09:59:09.745Z
    }

    //DateTimeFormatter：格式化或解析日期、时间类似于SimpleDateFormat
    @Test
    public void test3(){
        LocalDateTime localDateTime = LocalDateTime.now();
        //方式一:预定义的标准格式   ISO_DATE_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        //格式化
        String str1 = formatter.format(localDateTime);
        System.out.println(localDateTime);//2025-08-05T18:07:25.426
        System.out.println(str1);//2025-08-05T18:07:25.426
        //解析： 字符串-->日期
        TemporalAccessor parse = formatter.parse(str1);
        System.out.println(parse);

        //方式二：本地相关的格式  ofLocalizedDateTime()
        //LocalDateTime适用于：FormatStyle.SHORT/FormatStyle.MEDIUM/FormatStyle.LONG
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        //格式化
        String format = formatter1.format(localDateTime);
        System.out.println(format);//2025-8-5 18:15:43

        //本地相关的格式  ofLocalizedDate()
        //适用于LocalDate:FormatStyle.SHORT/FormatStyle.MEDIUM/FormatStyle.LONG/FormatStyle.FULL
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
        //格式化
        String format1 = formatter2.format(LocalDate.now());
        System.out.println(format1);//2025年8月5日 星期二

        //方式三：自定义格式。如：ofPattern("yyyy-MM-dd hh:mm:ss E")
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化
        String format2 = formatter3.format(LocalDateTime.now());
        System.out.println(format2);//2025-08-05 06:18:01

        //解析
        TemporalAccessor parse1 = formatter3.parse("2025-08-05 06:18:01");
        System.out.println(parse1);
    }

    @Test
    public void test4() throws InterruptedException {
        Instant instant = Instant.now();
        System.out.println(instant);
        Thread.sleep(1000);
        Instant instant2 = Instant.now();
        Duration between = Duration.between(instant, instant2);
        //获取秒和毫秒的方法是不一样的
        System.out.println("秒"+between.getSeconds());//秒1
        System.out.println("毫秒"+between.toMillis());//毫秒1008

        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime localDateTime1 = LocalDateTime.of(2025,8,6,01,02,03);
        Duration between1 = Duration.between(localDateTime, localDateTime1);
        System.out.println("小时"+between1.toHours());//6

        Period between2 = Period.between(LocalDate.now(), LocalDate.of(2025, 8, 6));
        System.out.println("日期间隔天数："+between2.getDays());//1

        ZoneId of = ZoneId.of("Europe/Berlin");
        ZoneId of1 = ZoneId.of("Brazil/East");
        System.out.println(of.getRules());
    }
}
